using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Security.Cryptography;

using PlugNT.Safe;
using PlugNT.Cms.Custom;
using PlugNT.Cms.Model;

namespace PlugNT.Cms.BLL
{



    public class Sys
    {

        private static readonly PlugNT.Cms.DAL.Sys dal = new PlugNT.Cms.DAL.Sys();
        private static SysInfo sysinfo = null;

        static Sys()
        {
            sysinfo = dal.GetSysInfo();


            sysinfo.str_filters = new Dictionary<string, bool>();
            if (sysinfo.str_filter != "")
            {

                sysinfo.str_filter = PlugNT.Common.Utils.ClearSpace(sysinfo.str_filter);
                StringBuilder sb = new StringBuilder();
                string[] filters = sysinfo.str_filter.Split(',');
                foreach (string s in filters)
                {
                    if (!sysinfo.str_filters.ContainsKey(s))
                    {
                        if (s.Contains("{"))
                        {
                            sysinfo.str_filters.Add(s.Replace("{", "(.{0,").Replace("}", "})"), true);
                        }
                        else
                        {
                            sysinfo.str_filters.Add(s, false);
                        }
                        sb.Append(s + ",");
                    }
                }
                sysinfo.str_filter = sb.ToString().Trim(',');
            }
        }






        public static SysInfo Info
        {
            get
            {
                return sysinfo;
            }
            set
            {
                sysinfo = value;
            }
        }






        public static bool Update()
        {
            if (sysinfo != null)
            {
                return dal.Update(sysinfo);
            }
            else
            {
                return false;
            }
        }





        public static bool UpdateCountAndTime()
        {
            if (sysinfo != null)
            {
                return dal.UpdateCountAndTime(sysinfo.id, sysinfo.updated_count, sysinfo.updated_time);
            }
            else
            {
                return false;
            }
        }





        public static bool UpdateStrfilter()
        {
            if (sysinfo != null)
            {
                return dal.UpdateStrfilter(sysinfo.id, sysinfo.str_filter);
            }
            else
            {
                return false;
            }
        }





        public static bool UpdateUsersadminhtml()
        {
            if (sysinfo != null)
            {
                return dal.UpdateUsersadminhtml(sysinfo.id, sysinfo.users_admin_html);
            }
            else
            {
                return false;
            }
        }





        public static bool UpdateNewusers()
        {
            if (sysinfo != null)
            {
                return dal.UpdateNewusers(sysinfo.id, sysinfo.new_users);
            }
            else
            {
                return false;
            }
        }






        public static bool UpdateMaxOnline()
        {
            if (sysinfo != null)
            {
                return dal.UpdateMaxOnline(sysinfo.id, sysinfo.max_online_count, sysinfo.max_online_time);
            }
            else
            {
                return false;
            }
        }







        public static bool CheckFilters(params string[] args)
        {
            foreach (System.Collections.Generic.KeyValuePair<string, bool> kv in sysinfo.str_filters)
            {

                if (kv.Value)
                {
                    foreach (string arg in args)
                    {
                        if (System.Text.RegularExpressions.Regex.IsMatch(arg, kv.Key))
                        {
                            return false;
                        }
                    }
                }
                else
                {
                    foreach (string arg in args)
                    {
                        if (arg.Contains(kv.Key))
                        {
                            return false;
                        }
                    }
                }
            }
            return true;
        }


    }
}

